package com.visionarysoftwaresolutions

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
class ProductImpl extends Product { }
@TestFor(ProductImpl)

class ProductTests {

    void testInvalidBlankName() {
       def prod = new ProductImpl(name:"", description:"cool", price:5.00)
       mockForConstraintsTests(ProductImpl, [prod])
       assert !prod.validate()
       assert "blank" == prod.errors["name"]
    }
    
    void testInvalidNonUniqueName() {
       def prod = new ProductImpl(name:"thing1", description:"cool", price:5.00)
       def prod2 = new ProductImpl(name:"thing1", description:"cool", price:5.00)
       mockForConstraintsTests(ProductImpl, [prod, prod2])
       prod.save()
       prod2.save()
       assert !prod2.validate()
       assert "unique" == prod.errors["name"]
    }
    
    void testInvalidBlankDescription() {
       def prod = new ProductImpl( name:"thing1",  description:"",  price:5.00)
       mockForConstraintsTests(ProductImpl, [prod])
       assert !prod.validate()
       assert "blank" == prod.errors["description"]
    }
    
    void testInvalidNegativePrice() {
       def prod = new ProductImpl(name:"thing1", price:-5.00)
       mockForConstraintsTests(ProductImpl, [prod])
       assert !prod.validate()
       assert "min" == prod.errors["price"]
    }
    
    void testValidProductNoImage() {
       def prod = new ProductImpl(name:"Jeremy Lin t-shirt", 
                              description:"Yellow Mamba!",
                              price:25.00)
       mockForConstraintsTests(ProductImpl, [prod])
       assert prod.validate()
    }
    
    void testValidProductWithImage() {
       def bits = [127, 56, 35]
       def pic = new Picture(image:bits)
       def prod = new ProductImpl(name:"Jeremy Lin t-shirt", 
                              description:"Yellow Mamba!",
                              price:25.00)
       mockForConstraintsTests(ProductImpl, [prod])
       prod.addToPictures(pic)
       assert prod.validate()
    }
}
